package org.stone.project.tx.handler.msgSendUserTypeHandler;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stone.project.tx.config.CtxHolderProxy;
import org.stone.project.tx.handle.MsgSendUserTypeHandler;
import org.stone.project.tx.model.Message;
import org.stone.project.tx.pojo.dto.UuidUserCtxDto;
import org.stone.project.tx.prop.TxProp;
import org.stone.project.tx.sysEnum.MsgType;
import org.stone.project.tx.sysEnum.ReceiveType;
import org.stone.project.tx.utils.ResultUtil;

import java.util.List;
import java.util.Map;

/**
 * 用户类别处理器
 */
@Service
public class UserMsgSendUserTypeHandler implements MsgSendUserTypeHandler {
    @Autowired
    private TxProp txProp;

    @Override
    public ReceiveType getReceiveType() {
        return ReceiveType.USER;
    }

    @Override
    public void sendMsg(Message msg) {
        Map<String, UuidUserCtxDto> receiveCtx = CtxHolderProxy.findByUserIdAll(msg.getReceiveId());
        MsgType msgType = MsgType.getInstance(msg.getContentType());
        if (ObjectUtil.isNotNull(receiveCtx)  && ObjectUtil.isNotNull(msgType)){
            receiveCtx.forEach((key,value)->{
                value.getCtx().writeAndFlush(ResultUtil.ok(msgType,msg.getContent()));
            });
        }
    }
}
